local skynet = require "skynet"
local m = require "skynet.manager"
local NAME = ...

skynet.init(function()
  skynet.error("[" .. NAME .. "] init")
end)

skynet.start(function()
  skynet.error("[" .. NAME .. "] start")

  skynet.dispatch("lua", function(session, source, cmd, ...)
    if cmd == "ping" then
      skynet.retpack("pong from " .. NAME)
    end
  end)

  if NAME == "A" then
    local b = skynet.localname(".B") or skynet.newservice("worker", "B")
    m.name(".B", b)

    skynet.sleep(10) -- 等待 B 启动

    local res = skynet.call(".B", "lua", "ping")
    skynet.error("[" .. NAME .. "] got response:", res)
  end
end)
